package com.example.hdwx.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hdwx.myapplication.R;
import com.example.hdwx.myapplication.base_activity.WebActivity;
import com.example.hdwx.myapplication.eneity.Cooklist_enetity;
import com.example.hdwx.myapplication.untils.ImageShow;

import java.util.ArrayList;
import java.util.List;

/**
 * 食谱recuclerview的适配器
 */

public class CookList_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<Cooklist_enetity> mdata =new ArrayList<>();
    Context mcontext;
    private ILoadCallback mCallback;

    public CookList_adapter(Context mcontext, List<Cooklist_enetity> list) {
        this.mcontext = mcontext;
        addData(list);
    }

    //对外暴露设置接口方法
    public void setLoadCallback(ILoadCallback callback) {
        this.mCallback = callback;
    }

    public void addData(List<Cooklist_enetity> list) {
        mdata.addAll(list);
        notifyDataSetChanged();//添加数据后通知 adpter 更新
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mdata.size()) {
            return R.layout.cooklist_item_view;
        }
        return R.layout.cooklist_recycleview_item;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mcontext).inflate(viewType, parent, false);
        if (viewType == R.layout.cooklist_recycleview_item){
            return new MyViewHolder(view);
        }else if (viewType == R.layout.cooklist_item_view){
            return new MycooklistItemHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
          if (holder instanceof MyViewHolder){
              final Cooklist_enetity cooklist = mdata.get(position);
              ((MyViewHolder)holder).tv1.setText(cooklist.getTitle());
              ((MyViewHolder)holder).tv2.setText(cooklist.getDescription());
              ((MyViewHolder)holder).tv3.setText(cooklist.getMaketime());
              ((MyViewHolder)holder).tv4.setText(cooklist.getClickCount());
              ((MyViewHolder)holder).tv5.setText(cooklist.getReleaseDate()+"");
              ((MyViewHolder)holder).tv6.setText(cooklist.getShareCount());
              ((MyViewHolder) holder).relativeLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent=new Intent(mcontext, WebActivity.class);
                      intent.putExtra("url",cooklist.getId());
                      mcontext.startActivity(intent);
                  }
              });

              new ImageShow().image_show(mcontext,cooklist.getImageUrl(),((MyViewHolder)holder).imageView);
          }else if (holder instanceof MycooklistItemHolder){
              Log.d("loadmore", "loading...");
              mCallback.onLoad();
          }
    }

    @Override
    public int getItemCount() {
       return mdata.size()+1;
    }


    //viewholder是用来查到找控件并方便在onbind方法中与实体类关联的，在viewholder中只需要差找到控件
    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tv1,tv2,tv3,tv4,tv5,tv6;
        RelativeLayout relativeLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.cooklist_recycleview_imageview);
            relativeLayout= (RelativeLayout) itemView.findViewById(R.id.cooklist_view);
            tv1= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv1);
            tv2= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv2);
            tv3= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv3);
            tv4= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv4);
            tv6= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv6);
            tv5= (TextView) itemView.findViewById(R.id.cooklist_recycleview_tv5);
        }
    }
     //加载更多
    class MycooklistItemHolder extends RecyclerView.ViewHolder{
        public MycooklistItemHolder(View itemView) {
            super(itemView);
        }
    }

    //回调接口，用于回调加载数据的方法
    public  interface ILoadCallback {
        void onLoad();
    }
}
